﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeiDuan.Model;
using WeiDuan.Service;
using B2B.Service;
using B2B.Business;
using B2B.Business.Impl;
using Autofac;

namespace B2B.Service.Impl
{
    [WeiDuanService(Name = "B2B.ProductService", Description = "商品管理")]
    public class ProductService : BaseService, IProductService, IAppService
    {
        private IProductBusiness mBusiness = null;

        public ProductService()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<B2B.Business.Impl.ProductBusiness>().As<IProductBusiness>();
            using (var container = builder.Build())
            {
                mBusiness = container.Resolve<IProductBusiness>();
            }
            base.eventCreate += ProductService_eventCreate;
        }

        void ProductService_eventCreate(IWeiDuanService service)
        {
            if (mBusiness is IServiceInjection)
                ((IServiceInjection)mBusiness).Injection(mWeiDuanService);
        }

        [WeiDuanInterface(Name = "QueryClassAttrByClass", Description = "根据类别查询属性列表")]
        public List<ClassAttrAndOptionsInfo> QueryClassAttrByClass(string id, UserInfo user)
        {
            return mBusiness.QueryClassAttrByClass(id, user);
        }

        [WeiDuanInterface(Name = "QueryClassAttrGroupByClass", Description = "根据类别查询属性列表，并对属性进行分组")]
        public List<ClassAttrGroupInfo> QueryClassAttrGroupByClass(string id, UserInfo user)
        {
            return mBusiness.QueryClassAttrGroupByClass(id, user);
        }

        [WeiDuanInterface(Name = "AddProduct", Description = "添加商品")]
        public string AddProduct(ProductAddRequest req, UserInfo user)
        {
            return mBusiness.AddProduct(req, user);
        }

        [WeiDuanInterface(Name = "UpdateProduct", Description = "修改商品")]
        public void UpdateProduct(ProductUpdateRequest req, UserInfo user)
        {
            mBusiness.UpdateProduct(req, user);
        }

        [WeiDuanInterface(Name = "RemoveProduct", Description = "删除商品")]
        public void RemoveProduct(string id, UserInfo user)
        {
            mBusiness.RemoveProduct(id, user);
        }

        [WeiDuanInterface(Name = "QueryProductByClass", Description = "根据类别查询商品")]
        public List<ProductInfo> QueryProductByClass(string id, UserInfo user)
        {
            return mBusiness.QueryProductByClass(id, user);
        }

        [WeiDuanInterface(Name = "QueryProductSKU", Description = "查询SKU")]
        public List<ProductSKUInfo> QueryProductSKU(ProductSKUQueryRequest req, UserInfo user)
        {
            return mBusiness.QueryProductSKU(req, user);
        }

        [WeiDuanInterface(Name = "GetProductSKUQueryArgs", Description = "返回商品查询的查询条件")]
        public List<QueryArg> GetProductSKUQueryArgs(UserInfo user)
        {
            return mBusiness.GetProductSKUQueryArgs(user);
        }

        [WeiDuanInterface(Name = "AddProductPack", Description = "添加报价")]
        public string AddProductPack(ProductPackAddRequest req, UserInfo user)
        {
            return mBusiness.AddProductPack(req, user);
        }

        [WeiDuanInterface(Name = "UpdateProductPack", Description = "修改报价")]
        public void UpdateProductPack(ProductPackUpdateRequest req, UserInfo user)
        {
            mBusiness.UpdateProductPack(req, user);
        }

        [WeiDuanInterface(Name = "SalerAudit", Description = "审核销售报价")]
        public void SalerAudit(ProductPackAuditRequest req, UserInfo user)
        {
            mBusiness.SalerAudit(req, user);
        }

        [WeiDuanInterface(Name = "BuyerAudit", Description = "确认采购报价")]
        public void BuyerAudit(ProductPackAuditRequest req, UserInfo user)
        {
            mBusiness.BuyerAudit(req, user);
        }

        [WeiDuanInterface(Name = "ReBuildSKU", Description = "重建SKU")]
        public void ReBuildSKU(string productId, UserInfo user)
        {
            mBusiness.ReBuildSKU(productId, user);
        }

        [WeiDuanInterface(Name = "AddBuyer", Description = "添加一个Buyer")]
        public string AddBuyer(BuyerAddRequest req, UserInfo user)
        {
            return mBusiness.AddBuyer(req, user);
        }

        [WeiDuanInterface(Name = "QueryBuyer", Description = "查询Buyer")]
        public List<BuyerInfo> QueryBuyer(BuyerQueryRequest req, UserInfo user)
        {
            return mBusiness.QueryBuyer(req, user);
        }

        [WeiDuanInterface(Name = "UpdateBuyer", Description = "修改Buyer")]
        public void UpdateBuyer(BuyerUpdateRequest req, UserInfo user)
        {
            mBusiness.UpdateBuyer(req, user);
        }

        [WeiDuanInterface(Name = "GetSaleType", Description = "获取销售类别")]
        public EnumValue[] GetSaleType()
        {
            return EnumHelper.EnumToArray(typeof(ProductPackSaleTypeEnum), o => o.GetDescription());
        }

        [WeiDuanInterface(Name = "RemoveBuyer", Description = "删除客户")]
        public void RemoveBuyer(string id, UserInfo user)
        {
            mBusiness.RemoveBuyer(id, user);
        }


        [WeiDuanInterface(Name = "QueryProductPackByClassID", Description = "根据指定客户、指定品类下的所有SKU报价")]
        public List<ProductPackSKUInfo> QueryProductPackByClassID(ProductPackQueryRequest req, UserInfo user)
        {
            return mBusiness.QueryProductPackByClassID(req, user);
        }

        [WeiDuanInterface(Name = "UpdateProductPackPrice", Description = "更新打包品报价")]
        public void UpdateProductPackPrice(ProductPackPriceUpdateRequest req, UserInfo user)
        {
            mBusiness.UpdateProductPackPrice(req, user);
        }

        [WeiDuanInterface(Name = "AuthBuyer", Description = "客户认证。")]
        public void AuthBuyer(BuyerAuthRequest req, UserInfo user)
        {
            mBusiness.AuthBuyer(req, user);
        }

        [WeiDuanInterface(Name = "QueryCarModelBySerialId", Description = "适用车型查询。")]
        public List<CarModelInfo> QueryCarModelBySerialId(string id, UserInfo user)
        {
            return mBusiness.QueryCarModelBySerialId(id, user);
        }
        
        [WebExcelInputInterface(Name = "AddProductSKU", Description = "批量SKU录入或导入。")]
        public void AddProductSKU(List<ProductSKUAddRequest> req, UserInfo user)
        {
            mBusiness.AddProductSKU(req, user);
        }

        [WebExcelInputInterface(Name = "AddCarPartsByExcel", Description = "汽车配置SKU录入或导入。")]
        public string AddCarPartsByExcel(List<ProductSKUInputByCarPartsRequest> req, UserInfo user)
        {
            string ret = mBusiness.AddCarPartsByExcel(req, user);
            if (string.IsNullOrEmpty(ret))
                ret = "导入成功。";

            return ret;
        }

        [WeiDuanInterface(Name = "QueryProductPageByClass", Description = "根据类别查询商品,分页查询。")]
        public QueryResult<List<ProductInfo>> QueryProductByClass(PageQueryRequest<string> req, UserInfo user)
        {
            return mBusiness.QueryProductByClass(req, user);
        }

        [WeiDuanInterface(Name = "QueryAttributeValue", Description = "查询指定属性下的所有值。")]
        public List<ProductAttributeValueInfo> QueryAttributeValue(string attrId, UserInfo user)
        {
            return mBusiness.QueryAttributeValue(attrId, user);
        }

        [WeiDuanInterface(Name = "QueryAttributeValueByCarUser", Description = "查询适用车型下的所有值。")]
        public List<ProductAttributeValueInfo> QueryAttributeValueByCarUser(UserInfo user)
        {
            return mBusiness.QueryAttributeValueByCarUser(user);
        }
    }
}
